package com.dev.dream.exochatlockscreen.slidemenu;

import java.io.IOException;
import java.util.ArrayList;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;
import android.widget.Toast;

import com.dev.dream.exochatlockscreen.EXOChatKeypadPreferences;
import com.dev.dream.exochatlockscreen.R;
import com.dev.dream.exochatlockscreen.service.SLockscreen;
import com.dev.dream.libs.adapter.ImageAdapterGridView;
import com.dev.dream.libs.adapter.ImageBitmap;
import com.dev.dream.libs.listviewanimation.simple.SwingBottomInAnimationAdapter;

public class BgThemeFragment extends Fragment {

	private static final int INITIAL_DELAY_MILLIS = 300;
	int size = 2;
	GridView gridView;
	ImageAdapterGridView customAdapterGridView;
	ArrayList<ImageBitmap> icons;

	

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {

		View rootView = inflater.inflate(R.layout.fragment_bg_theme, container,
				false);

		gridView = (GridView) rootView.findViewById(R.id.gv_bg_theme);
		icons = new ArrayList<ImageBitmap>();

		for (int i = 1; i <= EXOChatKeypadPreferences.getInstance().size_bg; i++) {
			icons.add(new ImageBitmap(i, getBitmapFromAssets("bg/bg" + i
					+ ".jpg", 256, 256), false));
		}

		customAdapterGridView = new ImageAdapterGridView(getActivity(), icons);

		SwingBottomInAnimationAdapter swingBottomInAnimationAdapter = new SwingBottomInAnimationAdapter(
				customAdapterGridView);
		swingBottomInAnimationAdapter.setAbsListView(gridView);

		assert swingBottomInAnimationAdapter.getViewAnimator() != null;
		swingBottomInAnimationAdapter.getViewAnimator().setInitialDelayMillis(
				INITIAL_DELAY_MILLIS);
		gridView.setAdapter(swingBottomInAnimationAdapter);

		gridView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				// TODO Auto-generated method stub

				if (EXOChatKeypadPreferences.getInstance().getImgIndexBg() != (arg2 + 1)) {
					SLockscreen.instance.setChangeBlurImage(true);
					EXOChatKeypadPreferences.getInstance().setImgIndexBg(arg2 + 1);
					Toast.makeText(getActivity(), "Change background succsess",
							1000).show();
				}

			}
		});

		return rootView;
	}

	public Bitmap getBitmapFromAssets(String fileName, int w, int h) {

		Bitmap bitmap = null;

		try {
			bitmap = BitmapFactory.decodeStream(getActivity().getAssets().open(
					fileName));

			bitmap = Bitmap.createScaledBitmap(bitmap, w, h, true);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return bitmap;
	}

	public Bitmap getBitmapFromAssets(String fileName) {

		Bitmap bitmap = null;

		try {
			bitmap = BitmapFactory.decodeStream(getActivity().getAssets().open(
					fileName));
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return bitmap;
	}

	Bitmap shrinkmethod(String file, int width, int height) {
		BitmapFactory.Options bitopt = new BitmapFactory.Options();
		bitopt.inJustDecodeBounds = true;
		Bitmap bit = BitmapFactory.decodeFile(file, bitopt);

		int h = (int) Math.ceil(bitopt.outHeight / (float) height);
		int w = (int) Math.ceil(bitopt.outWidth / (float) width);

		if (h > 1 || w > 1) {
			if (h > w) {
				bitopt.inSampleSize = h;

			} else {
				bitopt.inSampleSize = w;
			}
		}
		bitopt.inJustDecodeBounds = false;
		bit = BitmapFactory.decodeFile(file, bitopt);

		return bit;

	}
}
